package com.machine.filling.function.init.InputOilGunInfo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.function.init.OilGunList.OilGunListFragment;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.net.http.response.OilGunDetailResBean;
import com.machine.filling.net.http.response.OilTankListResBean;
import com.machine.filling.net.http.response.OilTypeListResBean;
import com.machine.filling.ui.dialog.ListDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/14 0014.
 * 添加油枪详情
 */

@SuppressLint("ValidFragment")
public class InputOilGunInfoFragment extends BaseFragment<InputOilGunInfoContract.Presenter> implements InputOilGunInfoContract.View, View.OnClickListener {

    private INextFragment INextFragment;
    private Button bt_save;
    private Button bt_cancle;

    private TextView tv_oil_type;
    private TextView tv_oil_tank;
    private EditText et_gun_name;
    private EditText et_gun_num;
    private EditText et_inital_nums;
    private EditText et_serial;

    private List<OilTypeListResBean.ResultBean.TypesBean> typeDatas;
    private ListDialog typeListDialog;
    private OilTypesAdapter oilTypesAdapter;

    private List<OilTankListResBean.ResultBean.PotsBean> tankDatas;
    private ListDialog tanksDialog;
    private OilTanksAdapter oilTanksAdapter;


    private String stationId;
    private String machineId;
    private String gunNum;
    private String gunName;
    private String initAmount;
    private String serialNum;


    private OilTypeListResBean.ResultBean.TypesBean typeBean;

    private OilTankListResBean.ResultBean.PotsBean tankBean;

    /**
     * @param mIntent
     */
    public InputOilGunInfoFragment(Intent mIntent) {
        super(mIntent);
    }

    @Override
    public View initViews() {
        View view = View.inflate(mContext, R.layout.fg_input_oil_gun_info,null);
        tv_oil_type = (TextView) view.findViewById(R.id.tv_oil_type);
        tv_oil_tank = (TextView) view.findViewById(R.id.tv_oil_tank);
        et_gun_name = (EditText) view.findViewById(R.id.et_gun_name);
        et_gun_num = (EditText) view.findViewById(R.id.et_gun_num);
        et_inital_nums = (EditText) view.findViewById(R.id.et_inital_nums);
        et_serial = (EditText) view.findViewById(R.id.et_serial);
        bt_save = (Button) view.findViewById(R.id.bt_save);
        bt_cancle = (Button) view.findViewById(R.id.bt_cancle);
        tv_oil_type.setOnClickListener(this);
        tv_oil_tank.setOnClickListener(this);
        bt_save.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {
        mPresenter = new InputOilGunInfoPresenter(mContext,this);
        mPresenter.getOilGunDetailInfo();
    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    /**
     * 获取油站id
     * @return
     */
    @Override
    public String getStationId() {
        return ClientInfoDao.getInstance().getModleByDeviceNum().getStationId();
    }

    /**
     * 获取油机id
     * @return
     */
    @Override
    public String getMachineId() {
        return ClientInfoDao.getInstance().getModleByDeviceNum().getMachineId();
    }

    @Override
    public OilTypeListResBean.ResultBean.TypesBean getTypeBean() {
        return typeBean;
    }

    @Override
    public OilTankListResBean.ResultBean.PotsBean getPotBean() {
        return tankBean;
    }

    /**
     * 获取油枪编号
     * @return
     */
    @Override
    public String getOilGunNum() {
        return et_gun_num.getText().toString().trim();
    }

    /**
     * 获取油枪id
     * @return
     */
    @Override
    public String getOilGunId() {
        return fragmentIntent.getStringExtra("gunId");
    }

    /**
     * 获取油枪名称
     * @return
     */
    @Override
    public String getOilGunName() {
        return et_gun_name.getText().toString().trim();
    }

    @Override
    public String getInitAccount() {
        return et_inital_nums.getText().toString().trim();
    }

    @Override
    public String getSerialNum() {
        return et_serial.getText().toString().trim();
    }


    /**
     * 显示油品列表
     * @param typesBeanList
     */
    @Override
    public void showOilTypeListResult(List<OilTypeListResBean.ResultBean.TypesBean> typesBeanList) {
        if (typeListDialog == null) {
            typeListDialog = new ListDialog(mContext);
            typeListDialog.setListTitle("选择油品");
            typeDatas = new ArrayList<>();
            typeDatas.addAll(typesBeanList);
            oilTypesAdapter = new OilTypesAdapter(mContext,typeDatas);
            oilTypesAdapter.setOnItemClickListener(new OilTypesAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(OilTypeListResBean.ResultBean.TypesBean itemBean, int position) {
                    tv_oil_type.setText(itemBean.getShortName() + " " + itemBean.getNum());
                    typeBean = itemBean;
                    typeListDialog.dismiss();
                }
            });
            typeListDialog.setAdapter(oilTypesAdapter);
        }else {
            typeDatas.clear();
            typeDatas.addAll(typesBeanList);
            oilTypesAdapter.notifyDataSetChanged();
        }
        typeListDialog.show();
    }

    /**
     * 显示油罐列表
     */
    @Override
    public void showOilTankListResult(List<OilTankListResBean.ResultBean.PotsBean> tankBeanList) {
        if (tanksDialog == null) {
            tanksDialog = new ListDialog(mContext);
            tanksDialog.setListTitle("选择油罐");
            tankDatas = new ArrayList<>();
            tankDatas.addAll(tankBeanList);
            oilTanksAdapter = new OilTanksAdapter(mContext,tankDatas);
            oilTanksAdapter.setOnItemClickListener(new OilTanksAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(OilTankListResBean.ResultBean.PotsBean itemBean, int position) {
                    tv_oil_tank.setText(itemBean.getName() + " [" + itemBean.getNum() + "]");
                    tankBean = itemBean;
                    tanksDialog.dismiss();
                }
            });
            tanksDialog.setAdapter(oilTanksAdapter);
        }else {
            tankDatas.clear();
            tankDatas.addAll(tankBeanList);
            oilTanksAdapter.notifyDataSetChanged();
        }
        tanksDialog.show();
    }

    /**
     * 显示油枪详情
     * @param gunDetailBean
     */
    @Override
    public void showOilGunDetailInfo(OilGunDetailResBean.ResultBean gunDetailBean) {
        stationId = gunDetailBean.getGun().getSellerId();
        machineId = gunDetailBean.getGun().getMachineId();
        gunNum = gunDetailBean.getGun().getNum();
        gunName = gunDetailBean.getGun().getName();
        initAmount = gunDetailBean.getGun().getInitAmount()+"";
        serialNum = gunDetailBean.getGun().getPortNum();

        tv_oil_type.setText(gunDetailBean.getGun().getTypeName() + " [" + gunDetailBean.getGun().getTypeNum() + "]");
        tv_oil_tank.setText(gunDetailBean.getGun().getPotName() + " [" + gunDetailBean.getGun().getPotNum() + "]");
        et_gun_name.setText(gunName);
        et_gun_num.setText(gunNum);
        et_inital_nums.setText(initAmount);
        et_serial.setText(serialNum);

        typeBean = new OilTypeListResBean.ResultBean.TypesBean();
        typeBean.setId(gunDetailBean.getGun().getTypeId());
        typeBean.setNum(gunDetailBean.getGun().getTypeNum());
        typeBean.setShortName(gunDetailBean.getGun().getTypeShortName());

        tankBean = new OilTankListResBean.ResultBean.PotsBean();
        tankBean.setId(gunDetailBean.getGun().getPotId());
        tankBean.setNum(gunDetailBean.getGun().getPotNum());
        tankBean.setName(gunDetailBean.getGun().getPotName());

    }

    @Override
    public void saveOilGunInfoResult() {
        INextFragment.replaceNext(new Intent(),OilGunListFragment.class.getName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //保存
            case R.id.bt_save:
                mPresenter.addOrModifyOilGunInfo();
                break;
            //取消
            case R.id.bt_cancle:
                INextFragment.replaceNext(new Intent(), OilGunListFragment.class.getName());
                break;
            //选择油品
            case R.id.tv_oil_type:
                mPresenter.getOilTypeList();
                break;
            //选择油罐
            case R.id.tv_oil_tank:
                mPresenter.getOilTankList();
                break;
        }
    }
}
